Program to Find the sum of 2nos using Stored functions
import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
CallableStatement cst=con.prepareCall(“{?=call sum1(?,?)}”);
cst.setInt(2,10);
cst.setInt(3,20);
cst.registerOutParameter(1,Types.INTEGER);
cst.executeUpdate();
int k=cst.getInt(1);
System.out.println(“Sum of 2nos”+k);
cst.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
Oracle Functions
create or replace function sum1(x in number, y in number )return number is
z number(5);
begin
z:=x+y;
return (z);
end;
/
Program to Find the simple interest using Stored functions
import java.sql.*;
import java.util.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
Scanner x=new Scanner(System.in);
int p,t,r,i;
System.out.println(“Enter p,t,r values”);
p=x.nextInt();
t=x.nextInt();
r=x.nextInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
CallableStatement cst=con.prepareCall(“{?=call interest(?,?,?)}”);
cst.setInt(2,p);
cst.setInt(3,t);
cst.setInt(4,r);
cst.registerOutParameter(1,Types.INTEGER);
cst.executeUpdate();
i=cst.getInt(1);
System.out.println(“Simple Interest”+i);
cst.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
Oracle Functions
create or replace function interest(x in number, y in number,z in number )
return number is
z number(5);
begin
z:=(x*y*z)/100;
return (z);
end;
/
Program to display the Salary for the given Employ no using stored functions
import java.sql.*;
import java.util.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
Scanner x=new Scanner(System.in);
int eno,sal;
System.out.println(“Enter Empno”);
eno=x.nextInt();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
CallableStatement cst=con.prepareCall(“{?=call employ(?)}”);
cst.setInt(2,eno);
cst.registerOutParameter(1,Types.INTEGER);
cst.executeUpdate();
sal=cst.getInt(1);
System.out.println(“Salary”+sal);
cst.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
Oracle Functions
create or replace function employ(x in number) return number is
z number(5);
begin
select sal into z from emp where eno=x;
return (z);
end;
/